package com.jasonchoi.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 直连交换机 一对一
 * 一个队列 对应多个消费者 也是轮询消费
 * @Author: JasonChoi
 * @Date: 2020/5/28 16:01
 */
@Configuration
public class DirectRabbitConfig {
    public static final String TEST_DIRECT_QUEUE = "TEST_DIRECT_QUEUE";
    public static final String TEST_DIRECT_EXCHANGE = "TEST_DIRECT_EXCHANGE";
    public static final String TEST_DIRECT_ROUTING = "TEST_DIRECT_ROUTING";
    /**
     * 队列 起名：TestDirectQueue
     * durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
     * exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
     * autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
     * @return
     */
    @Bean
    public Queue TestDirectQueue() {
        return new Queue(TEST_DIRECT_QUEUE,true,false,false);
    }

    /**
     * DirectExchange 直连型交换机
     * @return
     */
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange(TEST_DIRECT_EXCHANGE,true,false);
    }

    /**
     * 绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
     * @return
     */
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with(TEST_DIRECT_ROUTING);
    }



    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }



}
